package sgi.client;

import sgi.client.SGIPurchaseImageController.Source;
import sgi.details_new.ImageDetails;
import sgi.misc.FloatDimension;
import sgi.misc.InteractiveMapApplet;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
/**
 * Creates a frame in which to house the 
 * interactive map and adds space for extra buttons
 * */
public class SGIInteractiveMapUI extends SGIClientFrame {
	
	private static SGIInteractiveMapController interactiveMap;
	final JFrame frame;
	private List<ImageDetails> list;
	private JButton btnPurchaseImage, btnPurchaseSample;
	private JLabel lblNameplace, lblCoords, lblLatestDate;
	private ImageDetails imageDetails;
	/**Constructor. Receives list of images with locations and creates the frame and interactive map*/
	public SGIInteractiveMapUI(List<ImageDetails> locList) {
		list = locList;
		/**create a frame for the application*/
		frame = new JFrame("Interactive Map");
		/**make sure to shut down the application, when the frame is closed*/
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		
		/**create a panel for the applet and the button panel*/
		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.scrollbar);
		
		/**create a panel for the buttons*/
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		buttonPanel.setBounds(10, 606, 774, 155);
		
		
		//InteractiveMapApplet.setLocationList(list);
		final InteractiveMapApplet applet = new InteractiveMapApplet(list);
		
		applet.setBounds(0, 0, 800, 600);
		applet.setController(this);
		
		/**start the applet*/
		applet.init();
		
	/**Buttons*/
		/**create a button labeled "Back"*/
		JButton buttonClose = new JButton("Close");
		buttonClose.setBounds(663, 117, 99, 25);
		JButton buttonBackToCenter = new JButton("Center Map");
		buttonBackToCenter.setBounds(334, 13, 99, 25);
		
		/**add a tooltip*/
		buttonClose.setToolTipText("Close Interactive Map");
		buttonBackToCenter.setToolTipText("Pan map to (0,0)");
		
	/**give a name for the command*/
		/**if this is not assigned the actionCommand equals the button label*/
		buttonClose.setActionCommand("close map");
		buttonBackToCenter.setActionCommand("center map");
		
	/**create button label*/
		/**button actions*/
		buttonClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
				SGIInteractiveMapController.terminate();
			}
		});
		
		/**the BackToCenter button is simply linked to the applet*/
		buttonBackToCenter.addActionListener(applet);
		buttonPanel.setLayout(null);
		
		buttonPanel.add(buttonClose);
		buttonPanel.add(buttonBackToCenter);
		panel.setLayout(null);
		//store the applet in the panel
		panel.add(applet);
		//store the buttonPanelin panel
		panel.add(buttonPanel);
		
		JLabel lblLocation = new JLabel("Location: ");
		lblLocation.setBounds(12, 17, 56, 16);
		buttonPanel.add(lblLocation);
		
		lblNameplace = new JLabel("");
		lblNameplace.setBounds(80, 17, 242, 16);
		buttonPanel.add(lblNameplace);
		
		JLabel lblCoordinates = new JLabel("Coordinates: ");
		lblCoordinates.setBounds(22, 46, 82, 16);
		buttonPanel.add(lblCoordinates);
		
		lblCoords = new JLabel("");
		lblCoords.setBounds(131, 46, 227, 16);
		buttonPanel.add(lblCoords);
		
		btnPurchaseImage = new JButton("Purchase Image");
		btnPurchaseImage.setEnabled(false);
		btnPurchaseImage.setBounds(22, 117, 141, 25);
		buttonPanel.add(btnPurchaseImage);
		
		btnPurchaseSample = new JButton("Purchase Sample");
		btnPurchaseSample.setEnabled(false);
		btnPurchaseSample.setBounds(181, 117, 141, 25);
		buttonPanel.add(btnPurchaseSample);
		
		JLabel lblDate = new JLabel("Date: ");
		lblDate.setBounds(22, 75, 56, 16);
		buttonPanel.add(lblDate);
		
		lblLatestDate = new JLabel("");
		lblLatestDate.setBounds(131, 75, 227, 16);
		buttonPanel.add(lblLatestDate);
		
		btnPurchaseImage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg1) {
				try {
					SGIPurchaseImageController.create(imageDetails,Source.map);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		
		
		btnPurchaseSample.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg2) {
				try {
					SGIPurchaseSampleController.create(imageDetails,Source.map);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		
		
		/**store the panel in the frame*/
		frame.getContentPane().add(panel);
		/**assign a size for the frame*/
		/**reading the size from the applet*/
		frame.setSize(applet.getCurrSize().width, applet.getCurrSize().height);
		//System.out.println("X: " + applet.getSize().width + ", Y: " + (applet.getSize().height +200));
		
		/**display the frame*/
		frame.revalidate();
		frame.setVisible(true);
		frame.setSize(applet.getCurrSize().width, applet.getCurrSize().height +200);
		//frame.revalidate();
		//PApplet.main(new String[] { mapTest1.class.getName() });
		
	}
	
	/*public static void setLocationList (List<ImageDetails> list) {
		this.list = list;
		//InteractiveMapApplet.setLocationList(list.iterator(), list.size());
		
	}*/
	
	public void setVisible(Boolean b) {
		frame.setVisible(b);
	}
	
	/**Activates the buttons and sets them to redirect to correct image/sample purchase*/
	public void setActive(ImageDetails imgdtls) {
		if (imageDetails == null)
			imageDetails = new ImageDetails(imgdtls);
		else
		{
			imageDetails.copyFrom(imgdtls);
		}
		btnPurchaseImage.setEnabled(true);
		btnPurchaseSample.setEnabled(true);
		//btnReviewgetInfo.setEnabled(true);
		lblNameplace.setText(imgdtls.getLocation());
		lblCoords.setText(imgdtls.getCoords().getX() + ", " + imgdtls.getCoords().getY());
		lblLatestDate.setText(imgdtls.getDate());
		
	}
	/**De-activates buttons when no markers are selected*/
	public void setInactive() {
		btnPurchaseImage.setEnabled(false);
		btnPurchaseSample.setEnabled(false);
		//btnReviewgetInfo.setEnabled(false);
		lblNameplace.setText("");
		lblCoords.setText("");
		lblLatestDate.setText("");
	}
	
	public void setController(SGIInteractiveMapController interactiveMap) {
		SGIInteractiveMapUI.interactiveMap = interactiveMap;
	}
}

